(function() {
  var $, FormController, errorBoxCssClass, exports, fadeAwayAfter, hideError, showError, validationErrorCssClass;
  var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
    for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
    function ctor() { this.constructor = child; }
    ctor.prototype = parent.prototype;
    child.prototype = new ctor;
    child.__super__ = parent.prototype;
    return child;
  }, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  exports = this;
  $ = jQuery;
  validationErrorCssClass = '.field-validation-error';
  errorBoxCssClass = '.error-box';
  fadeAwayAfter = 7 * 1000;
  showError = function(element, message) {
    var error;
    error = (element.siblings(validationErrorCssClass)).first();
    if (message != null) {
      error.text(message);
    }
    return error.hide().fadeIn('normal', function() {
      return exports.setTimeout(function() {
        return error.fadeOut('normal');
      }, fadeAwayAfter);
    });
  };
  hideError = function(element) {
    return ($(element)).siblings(validationErrorCssClass).first().fadeOut();
  };
  FormController = (function() {
    __extends(FormController, Spine.Controller);
    FormController.elements = {
      'form': 'form'
    };
    FormController.events = {
      'submit form': 'submit'
    };
    function FormController() {
      FormController.__super__.constructor.apply(this, arguments);
      this.hideErrors();
      this.form.delegate("input, textarea, select, " + validationErrorCssClass, 'vclick', __bind(function(event) {
        return hideError(event.currentTarget);
      }, this));
    }
    FormController.prototype.activate = function() {
      var _ref;
      return (_ref = ($('input, textarea, select', this.form)).first()) != null ? _ref.focus() : void 0;
    };
    FormController.prototype.reset = function() {
      this.hideErrors();
      return this.form.get(0).reset();
    };
    FormController.prototype.showErrors = function(errors) {
      var currentError, error, errorBox, firstError, formErrors, html, message, _i, _j, _k, _len, _len2, _len3, _ref;
      formErrors = [];
      firstError = null;
      for (_i = 0, _len = errors.length; _i < _len; _i++) {
        error = errors[_i];
        _ref = error.messages;
        for (_j = 0, _len2 = _ref.length; _j < _len2; _j++) {
          message = _ref[_j];
          if (error.name === '') {
            formErrors.push(message);
          } else {
            currentError = ($("input[name=" + error.name + "], textarea[name=" + error.name + "], select[name=" + error.name + "]", this.form)).first();
            if (firstError == null) {
              firstError = currentError;
            }
            showError(currentError, message);
          }
        }
      }
      if (firstError != null) {
        $.mobile.silentScroll(firstError.parent().children(validationErrorCssClass).offset().top);
      }
      if (!formErrors.length) {
        return 0;
      }
      if (formErrors.length === 1) {
        html = "<p>" + formErrors[0] + "</p>";
      } else {
        html = 'Please correct the following errors: <ol>';
        for (_k = 0, _len3 = formErrors.length; _k < _len3; _k++) {
          message = formErrors[_k];
          html += "<li>" + message + "</li>";
        }
        html += '</ol>';
      }
      ($('.error-box', this.form)).remove();
      errorBox = $("<div class='" + errorBoxCssClass + " ui-body ui-corner-all                     ui-body-e'>" + html + "</div>");
      return errorBox.insertBefore($('div[data-role=fieldcontain]:last', this.form)).fadeIn('normal', function() {
        return exports.setTimeout(function() {
          return errorBox.fadeOut('normal', function() {
            return errorBox.remove();
          });
        }, fadeAwayAfter);
      });
    };
    FormController.prototype.hideErrors = function() {
      ($(validationErrorCssClass, this.form)).hide();
      return ($(errorBoxCssClass, this.form)).hide().remove();
    };
    return FormController;
  })();
  exports.TomatoBreak.FormController = FormController;
}).call(this);
